function $(id){
  return document.getElementById(id);
}
/**
 * aqiData，存储用户输入的空气指数数据
 * 示例格式：
 * aqiData = {
 *    "北京": 90,
 *    "上海": 40
 * };
 */
var aqiData = {};


function addAqiData() {
  let city = $('aqi-city-input').value;
  let value = $('aqi-value-input').value;
  if (!city.match(/^[A-Za-z\u4E00-\u9FA5]+$/)) {
    alert("城市名必须为中英文字符！");
    return;
  }
  if (!value.match(/^\d+$/)) {
      alert("空气质量指数必须为整数！");
      return;
  }
  aqiData[city] = value;
  alert('添加成功');
  $('aqi-city-input').value = '';
  $('aqi-value-input').value = '';
}


function renderAqiList() {
  let html = '';
  for(let key in aqiData){
    html += `<tr>
              <td>${key}</td><td>${aqiData[key]}</td><td><button>删除</button></td>
            </tr>`
  }
  $('aqi-table').innerHTML = html;

}


function addBtnHandle() {
  addAqiData();
  renderAqiList();
}


function delBtnHandle() {
  renderAqiList();
}

function init() {
  $('add-btn').addEventListener('click', addBtnHandle,false);
  $('aqi-table').addEventListener('click',(e)=>{
    if(e.target.nodeName.toLowerCase() == 'button'){
       let self = e.target.parentElement.parentElement;
       let selfPart = e.target.parentElement.parentElement.parentElement;
       selfPart.removeChild(self);
    }
  },false)
}

init();